<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>36.4. Query Language</title>
<link rel="stylesheet" href="dbstyle.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
<link rel="start" href="index.html" title="Programmer's Reference Guide">
<link rel="up" href="zend.search.lucene.html" title="Chapter 36. Zend_Search_Lucene">
<link rel="prev" href="zend.search.lucene.searching.html" title="36.3. Searching an Index">
<link rel="next" href="zend.search.lucene.query-api.html" title="36.5. Query Construction API">
<link rel="chapter" href="introduction.html" title="Chapter 1. Introduction to Zend Framework">
<link rel="chapter" href="zend.acl.html" title="Chapter 2. Zend_Acl">
<link rel="chapter" href="zend.auth.html" title="Chapter 3. Zend_Auth">
<link rel="chapter" href="zend.cache.html" title="Chapter 4. Zend_Cache">
<link rel="chapter" href="zend.config.html" title="Chapter 5. Zend_Config">
<link rel="chapter" href="zend.console.getopt.html" title="Chapter 6. Zend_Console_Getopt">
<link rel="chapter" href="zend.controller.html" title="Chapter 7. Zend_Controller">
<link rel="chapter" href="zend.currency.html" title="Chapter 8. Zend_Currency">
<link rel="chapter" href="zend.date.html" title="Chapter 9. Zend_Date">
<link rel="chapter" href="zend.db.html" title="Chapter 10. Zend_Db">
<link rel="chapter" href="zend.debug.html" title="Chapter 11. Zend_Debug">
<link rel="chapter" href="zend.dojo.html" title="Chapter 12. Zend_Dojo">
<link rel="chapter" href="zend.dom.html" title="Chapter 13. Zend_Dom">
<link rel="chapter" href="zend.exception.html" title="Chapter 14. Zend_Exception">
<link rel="chapter" href="zend.feed.html" title="Chapter 15. Zend_Feed">
<link rel="chapter" href="zend.filter.html" title="Chapter 16. Zend_Filter">
<link rel="chapter" href="zend.form.html" title="Chapter 17. Zend_Form">
<link rel="chapter" href="zend.gdata.html" title="Chapter 18. Zend_Gdata">
<link rel="chapter" href="zend.http.html" title="Chapter 19. Zend_Http">
<link rel="chapter" href="zend.infocard.html" title="Chapter 20. Zend_InfoCard">
<link rel="chapter" href="zend.json.html" title="Chapter 21. Zend_Json">
<link rel="chapter" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="chapter" href="zend.ldap.html" title="Chapter 23. Zend_Ldap">
<link rel="chapter" href="zend.loader.html" title="Chapter 24. Zend_Loader">
<link rel="chapter" href="zend.locale.html" title="Chapter 25. Zend_Locale">
<link rel="chapter" href="zend.log.html" title="Chapter 26. Zend_Log">
<link rel="chapter" href="zend.mail.html" title="Chapter 27. Zend_Mail">
<link rel="chapter" href="zend.measure.html" title="Chapter 28. Zend_Measure">
<link rel="chapter" href="zend.memory.html" title="Chapter 29. Zend_Memory">
<link rel="chapter" href="zend.mime.html" title="Chapter 30. Zend_Mime">
<link rel="chapter" href="zend.openid.html" title="Chapter 31. Zend_OpenId">
<link rel="chapter" href="zend.paginator.html" title="Chapter 32. Zend_Paginator">
<link rel="chapter" href="zend.pdf.html" title="Chapter 33. Zend_Pdf">
<link rel="chapter" href="zend.registry.html" title="Chapter 34. Zend_Registry">
<link rel="chapter" href="zend.rest.html" title="Chapter 35. Zend_Rest">
<link rel="chapter" href="zend.search.lucene.html" title="Chapter 36. Zend_Search_Lucene">
<link rel="chapter" href="zend.server.html" title="Chapter 37. Zend_Server">
<link rel="chapter" href="zend.service.html" title="Chapter 38. Zend_Service">
<link rel="chapter" href="zend.session.html" title="Chapter 39. Zend_Session">
<link rel="chapter" href="zend.soap.html" title="Chapter 40. Zend_Soap">
<link rel="chapter" href="zend.test.html" title="Chapter 41. Zend_Test">
<link rel="chapter" href="zend.text.html" title="Chapter 42. Zend_Text">
<link rel="chapter" href="zend.timesync.html" title="Chapter 43. Zend_TimeSync">
<link rel="chapter" href="zend.translate.html" title="Chapter 44. Zend_Translate">
<link rel="chapter" href="zend.uri.html" title="Chapter 45. Zend_Uri">
<link rel="chapter" href="zend.validate.html" title="Chapter 46. Zend_Validate">
<link rel="chapter" href="zend.version.html" title="Chapter 47. Zend_Version">
<link rel="chapter" href="zend.view.html" title="Chapter 48. Zend_View">
<link rel="chapter" href="zend.xmlrpc.html" title="Chapter 49. Zend_XmlRpc">
<link rel="appendix" href="requirements.html" title="Appendix A. Zend Framework Requirements">
<link rel="appendix" href="coding-standard.html" title="Appendix B. Zend Framework Coding Standard for PHP">
<link rel="appendix" href="copyrights.html" title="Appendix C. Copyright Information">
<link rel="index" href="the.index.html" title="Index">
<link rel="subsection" href="zend.search.lucene.query-language.html#zend.search.lucene.query-language.terms" title="36.4.1. Terms">
<link rel="subsection" href="zend.search.lucene.query-language.html#zend.search.lucene.query-language.fields" title="36.4.2. Fields">
<link rel="subsection" href="zend.search.lucene.query-language.html#zend.search.lucene.query-language.wildcard" title="36.4.3. Wildcards">
<link rel="subsection" href="zend.search.lucene.query-language.html#zend.search.lucene.query-language.modifiers" title="36.4.4. Term Modifiers">
<link rel="subsection" href="zend.search.lucene.query-language.html#zend.search.lucene.query-language.range" title="36.4.5. Range Searches">
<link rel="subsection" href="zend.search.lucene.query-language.html#zend.search.lucene.query-language.fuzzy" title="36.4.6. Fuzzy Searches">
<link rel="subsection" href="zend.search.lucene.query-language.html#zend.search.lucene.query-language.proximity-search" title="36.4.7. Proximity Searches">
<link rel="subsection" href="zend.search.lucene.query-language.html#zend.search.lucene.query-language.boosting" title="36.4.8. Boosting a Term">
<link rel="subsection" href="zend.search.lucene.query-language.html#zend.search.lucene.query-language.boolean" title="36.4.9. Boolean Operators">
<link rel="subsection" href="zend.search.lucene.query-language.html#zend.search.lucene.query-language.grouping" title="36.4.10. Grouping">
<link rel="subsection" href="zend.search.lucene.query-language.html#zend.search.lucene.query-language.field-grouping" title="36.4.11. Field Grouping">
<link rel="subsection" href="zend.search.lucene.query-language.html#zend.search.lucene.query-language.escaping" title="36.4.12. Escaping Special Characters">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader"><table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">36.4. Query Language</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="zend.search.lucene.searching.html">Prev</a> </td>
<th width="60%" align="center">Chapter 36. Zend_Search_Lucene</th>
<td width="20%" align="right"> <a accesskey="n" href="zend.search.lucene.query-api.html">Next</a>
</td>
</tr>
</table></div>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="zend.search.lucene.query-language"></a>36.4. Query Language</h2></div></div></div>
<p>
        Java Lucene and Zend_Search_Lucene provide quite powerful query languages.
    </p>
<p>
        These languages are mostly the same with some minor differences, which are mentioned below.
    </p>
<p>
        Full Java Lucene query language syntax documentation can be found
        <a href="http://lucene.apache.org/java/docs/queryparsersyntax.html" target="_top">here</a>.
    </p>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.search.lucene.query-language.terms"></a>36.4.1. Terms</h3></div></div></div>
<p>
            A query is broken up into terms and operators. There are three types of terms: Single Terms, Phrases,
            and Subqueries.
        </p>
<p>
            A Single Term is a single word such as "test" or "hello".
        </p>
<p>
            A Phrase is a group of words surrounded by double quotes such as "hello dolly".
        </p>
<p>
            A Subquery is a query surrounded by parentheses such as "(hello dolly)".
        </p>
<p>
            Multiple terms can be combined together with boolean operators to form complex queries (see below).
        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.search.lucene.query-language.fields"></a>36.4.2. Fields</h3></div></div></div>
<p>
            Lucene supports fields of data. When performing a search you can either specify a field, or use
            the default field. The field names depend on indexed data and default field is defined
            by current settings.
        </p>
<p>
            The first and most significant difference from Java Lucene is that terms are searched through
            <span class="emphasis"><em>all fields</em></span> by default.
        </p>
<p>
            There are two static methods in the Zend_Search_Lucene class which allow the developer to configure these settings:
        </p>
<pre class="programlisting">&lt;?php
$defaultSearchField = Zend_Search_Lucene::getDefaultSearchField();
...
Zend_Search_Lucene::setDefaultSearchField('contents');
        </pre>
<p>
            The <code class="code">null</code> value indicated that the search is performed across all fields. It's the default setting.
        </p>
<p>
            You can search specific fields by typing the field name followed by a colon ":" followed by the term you
            are looking for.
        </p>
<p>
            As an example, let's assume a Lucene index contains two fields- title and text- with text as the default field.
            If you want to find the document entitled "The Right Way" which contains the text "don't go this way",
            you can enter:
        </p>
<pre class="programlisting">title:"The Right Way" AND text:go
        </pre>
<p>
            or
        </p>
<pre class="programlisting">title:"Do it right" AND go
        </pre>
<p>
            Because "text" is the default field, the field indicator is not required.
        </p>
<p>
            Note: The field is only valid for the term, phrase or subquery that it directly precedes,
            so the query
            </p>
<pre class="programlisting">title:Do it right
            </pre>
<p>
            Will only find "Do" in the title field. It will find "it" and "right" in the default field (if the default field is set)
            or in all indexed fields (if the default field is set to <code class="code">null</code>).
        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.search.lucene.query-language.wildcard"></a>36.4.3. Wildcards</h3></div></div></div>
<p>
            Lucene supports single and multiple character wildcard searches within single terms (but not within phrase queries).
        </p>
<p>
            To perform a single character wildcard search use the "?" symbol.
        </p>
<p>
            To perform a multiple character wildcard search use the "*" symbol.
        </p>
<p>
            The single character wildcard search looks for string that match the term with the "?" replaced by any single character.
            For example, to search for "text" or "test" you can use the search:
            </p>
<pre class="programlisting">te?t
            </pre>
<p>
        </p>
<p>
            Multiple character wildcard searches look for 0 or more characters when matching strings against terms. For example, to search for test,
            tests or tester, you can use the search:
            </p>
<pre class="programlisting">test*
            </pre>
<p>
        </p>
<p>
            You can use "?", "*" or both at any place of the term:
            </p>
<pre class="programlisting">*wr?t*
            </pre>
<p>
            It searches for "write", "wrote", "written", "rewrite", "rewrote" and so on.
        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.search.lucene.query-language.modifiers"></a>36.4.4. Term Modifiers</h3></div></div></div>
<p>
            Lucene supports modifying query terms to provide a wide range of searching options.
        </p>
<p>
            "~" modifier can be used to specify proximity search for phrases or fuzzy search for individual terms.
        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.search.lucene.query-language.range"></a>36.4.5. Range Searches</h3></div></div></div>
<p>
            Range queries allow the developer or user to match documents whose field(s) values are between the lower and upper bound specified by the range query.
            Range Queries can be inclusive or exclusive of the upper and lower bounds. Sorting is performed lexicographically.
            </p>
<pre class="programlisting">mod_date:[20020101 TO 20030101]
            </pre>
<p>
            This will find documents whose mod_date fields have values between 20020101 and 20030101, inclusive. Note that Range Queries are not
            reserved for date fields. You could also use range queries with non-date fields:
            </p>
<pre class="programlisting">title:{Aida TO Carmen}
            </pre>
<p>
            This will find all documents whose titles would be sorted between Aida and Carmen, but not including Aida and Carmen.
        </p>
<p>
            Inclusive range queries are denoted by square brackets. Exclusive range queries are denoted by curly brackets.
        </p>
<p>
            If field is not specified then Zend_Search_Lucene searches for specified interval through all fields by default.
            </p>
<pre class="programlisting">{Aida TO Carmen}
            </pre>
<p>
        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.search.lucene.query-language.fuzzy"></a>36.4.6. Fuzzy Searches</h3></div></div></div>
<p>
            Zend_Search_Lucene as well as Java Lucene supports fuzzy searches based on the Levenshtein Distance, or Edit Distance algorithm. 
            To do a fuzzy search use the tilde, "~", symbol at the end of a Single word Term. For example to search for a term similar 
            in spelling to "roam" use the fuzzy search:

            </p>
<pre class="programlisting">roam~</pre>
<p>

            This search will find terms like foam and roams.

            Additional (optional) parameter can specify the required similarity. The value is between 0 and 1, with a value closer to 1 only terms 
            with a higher similarity will be matched. For example:

            </p>
<pre class="programlisting">roam~0.8</pre>
<p>

            The default that is used if the parameter is not given is 0.5.
        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.search.lucene.query-language.proximity-search"></a>36.4.7. Proximity Searches</h3></div></div></div>
<p>
            Lucene supports finding words from a phrase that are within a specified word distance in a string. To do a proximity search
            use the tilde, "~", symbol at the end of the phrase. For example to search for a "Zend" and
            "Framework" within 10 words of each other in a document use the search:
            </p>
<pre class="programlisting">"Zend Framework"~10
            </pre>
<p>
        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.search.lucene.query-language.boosting"></a>36.4.8. Boosting a Term</h3></div></div></div>
<p>
            Java Lucene and Zend_Search_Lucene provide the relevance level of matching documents based
            on the terms found. To boost the relevance of a term use the caret, "^", symbol with a boost factor (a number)
            at the end of the term you are searching. The higher the boost factor, the more relevant
            the term will be.
        </p>
<p>
            Boosting allows you to control the relevance of a document by boosting individual terms. For example,
            if you are searching for
            </p>
<pre class="programlisting">PHP framework
            </pre>
<p>
            and you want the term "PHP" to be more relevant boost it using the ^ symbol along with the
            boost factor next to the term. You would type:
            </p>
<pre class="programlisting">PHP^4 framework
            </pre>
<p>
            This will make documents with the term PHP appear more relevant. You can also boost phrase
            terms and subqueries as in the example:
            </p>
<pre class="programlisting">"PHP framework"^4 "Zend Framework"
            </pre>
<p>
            By default, the boost factor is 1. Although the boost factor must be positive,
            it may be less than 1 (e.g. 0.2).
        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.search.lucene.query-language.boolean"></a>36.4.9. Boolean Operators</h3></div></div></div>
<p>
            Boolean operators allow terms to be combined through logic operators.
            Lucene supports AND, "+", OR, NOT and "-" as Boolean operators.
            Java Lucene requires boolean operators to be ALL CAPS. Zend_Search_Lucene does not.
        </p>
<p>
            AND, OR, and NOT operators and "+", "-" defines two different styles to construct boolean queries.
            Unlike Java Lucene, Zend_Search_Lucene doesn't allow these two styles to be mixed.
        </p>
<p>
            If the AND/OR/NOT style is used, then an AND or OR operator must be present between all query terms.
            Each term may also be preceded by NOT operator. The AND operator has higher precedence than the OR operator.
            This differs from Java Lucene behavior.
        </p>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.search.lucene.query-language.boolean.and"></a>36.4.9.1. AND</h4></div></div></div>
<p>
                The AND operator means that all terms in the "AND group" must match some part of the searched field(s).
            </p>
<p>
                To search for documents that contain "PHP framework" and "Zend Framework" use the query:
                </p>
<pre class="programlisting">"PHP framework" AND "Zend Framework"
                </pre>
<p>
            </p>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.search.lucene.query-language.boolean.or"></a>36.4.9.2. OR</h4></div></div></div>
<p>
                The OR operator divides the query into several optional terms.
            </p>
<p>
                To search for documents that contain "PHP framework" or "Zend Framework" use the query:
                </p>
<pre class="programlisting">"PHP framework" OR "Zend Framework"
                </pre>
<p>
            </p>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.search.lucene.query-language.boolean.not"></a>36.4.9.3. NOT</h4></div></div></div>
<p>
                The NOT operator excludes documents that contain the term after NOT. But an "AND group" which contains
                only terms with the NOT operator gives an empty result set instead of a full set of indexed documents.
            </p>
<p>
                To search for documents that contain "PHP framework" but not "Zend Framework" use the query:
                </p>
<pre class="programlisting">"PHP framework" AND NOT "Zend Framework"
                </pre>
<p>
            </p>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.search.lucene.query-language.boolean.other-form"></a>36.4.9.4. &amp;&amp;, ||, and ! operators</h4></div></div></div>
<p>
                &amp;&amp;, ||, and ! may be used instead of AND, OR, and NOT notation.
            </p>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.search.lucene.query-language.boolean.plus"></a>36.4.9.5. +</h4></div></div></div>
<p>
                The "+" or required operator stipulates that the term after the "+" symbol must match the document.
            </p>
<p>
                To search for documents that must contain "Zend" and may contain "Framework" use the query:
                </p>
<pre class="programlisting">+Zend Framework
                </pre>
<p>
            </p>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.search.lucene.query-language.boolean.minus"></a>36.4.9.6. -</h4></div></div></div>
<p>
                The "-" or prohibit operator excludes documents that match the term after the "-" symbol.
            </p>
<p>
                To search for documents that contain "PHP framework" but not "Zend Framework" use the query:
                </p>
<pre class="programlisting">"PHP framework" -"Zend Framework"
                </pre>
<p>
            </p>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.search.lucene.query-language.boolean.no-operator"></a>36.4.9.7. No Operator</h4></div></div></div>
<p>
                If no operator is used, then the search behavior is defined by the "default boolean operator".
            </p>
<p>
                This is set to <code class="code">OR</code> by default.
            </p>
<p>
                That implies each term is optional by default. It may or may not be present within document, but documents with this term
                will recieve a higher score.
            </p>
<p>
                To search for documents that requires "PHP framework" and may contain "Zend Framework" use the query:
                </p>
<pre class="programlisting">+"PHP framework" "Zend Framework"
                </pre>
<p>
            </p>
<p>
                The default boolean operator may be set or retrieved with the
                <code class="code">Zend_Search_Lucene_Search_QueryParser::setDefaultOperator($operator)</code> and
                <code class="code">Zend_Search_Lucene_Search_QueryParser::getDefaultOperator()</code> methods, respectively.
            </p>
<p>
                These methods operate with the
                <code class="code">Zend_Search_Lucene_Search_QueryParser::B_AND</code> and
                <code class="code">Zend_Search_Lucene_Search_QueryParser::B_OR</code> constants.
            </p>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.search.lucene.query-language.grouping"></a>36.4.10. Grouping</h3></div></div></div>
<p>
            Java Lucene and Zend_Search_Lucene support using parentheses to group clauses to form sub queries. This can be
            useful if you want to control the precedence of boolean logic operators for a query or mix different boolean query styles:
            </p>
<pre class="programlisting">+(framework OR library) +php
            </pre>
<p>
            Zend_Search_Lucene supports subqueries nested to any level.
        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.search.lucene.query-language.field-grouping"></a>36.4.11. Field Grouping</h3></div></div></div>
<p>
            Lucene also supports using parentheses to group multiple clauses to a single field.
        </p>
<p>
            To search for a title that contains both the word "return" and the phrase "pink panther" use the query:
            </p>
<pre class="programlisting">title:(+return +"pink panther")
            </pre>
<p>
        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.search.lucene.query-language.escaping"></a>36.4.12. Escaping Special Characters</h3></div></div></div>
<p>
            Lucene supports escaping special characters that are used in query syntax. The current list of special
            characters is:
        </p>
<p>
            + - &amp;&amp; || ! ( ) { } [ ] ^ " ~ * ? : \
        </p>
<p>
            + and - inside single terms are automatically treated as common characters.
        </p>
<p>
            For other instances of these characters use the \ before each special character you'd like to escape. For example to search for (1+1):2 use the query:
            </p>
<pre class="programlisting">\(1\+1\)\:2
            </pre>
<p>
        </p>
</div>
</div>
<div class="navfooter"><table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="zend.search.lucene.searching.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="zend.search.lucene.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="zend.search.lucene.query-api.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">36.3. Searching an Index </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> 36.5. Query Construction API</td>
</tr>
</table></div>
<div class="revinfo"></div>
</body>
</html>
